package com.gxuwz.medical.domain.baoxiao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.gxuwz.medical.dao.BaoXiaoDao;
import com.gxuwz.medical.dao.FamilyDao;
import com.gxuwz.medical.dao.PersonDao;
import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.domain.person.Person;



public class BaoxiaoStatistic {
	private Family family;
	private Person person;
	private BaoXiao baoxiao;
	public BaoxiaoStatistic(Family family, Person person, BaoXiao baoxiao) {
		this.family=family;
		this.baoxiao=baoxiao;
		this.person=person;
	}
	public BaoxiaoStatistic(){}
	public List<BaoxiaoStatistic> statistic(String fullname,String illname,Date stime,Date etime,String areacode) {
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		List list = new ArrayList();
		DbUtil db = new DbUtil();
		try {
			conn = db.getConn();
			String sql="select * from family f,person p,baoxiao b where "
					+ "f.familycode=p.familycode and p.idcard=b.idcard "
					+ "and fullname like ? and illname like ? and baoxiaotime>? and baoxiaotime<? "
					+ "and (town like ? or village like ? or `group` like ?)";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, fullname+"%");
			pstmt.setString(2, illname+"%");
			if(stime!=null)
				pstmt.setTimestamp(3, new java.sql.Timestamp(stime.getTime()));
			else
				pstmt.setString(3, "0000-00-00 00:00:00");
			if(etime!=null)
				pstmt.setTimestamp(4, new java.sql.Timestamp(etime.getTime()));
			else
				pstmt.setString(4, "9999-12-31 23:59:59");
			pstmt.setString(5, areacode+"%");
			pstmt.setString(6, areacode+"%");
			pstmt.setString(7, areacode+"%");
			rs = pstmt.executeQuery();
			while(rs.next()){
				Family f = new FamilyDao().handle(rs);
				Person p = new PersonDao().handle(rs);
				BaoXiao b = new BaoXiaoDao().handle(rs);
				BaoxiaoStatistic bs = new BaoxiaoStatistic(f,p,b);
				list.add(bs);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	public Family getFamily() {
		return family;
	}
	public void setFamily(Family family) {
		this.family = family;
	}
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	public BaoXiao getBaoxiao() {
		return baoxiao;
	}
	public void setBaoxiao(BaoXiao baoxiao) {
		this.baoxiao = baoxiao;
	}

	
}	
